//
//  MovieDetailViewController.m
//  豆瓣(阿黄)
//
//  Created by dllo on 15/10/31.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "MovieDetailView.h"
#import "AppTools.h"
#import "UIImageView+WebCache.h"
#import "MovieDetail.h"
#import "UserLoginViewController.h"
#import "DataBase.h"

@interface MovieDetailViewController ()

@property (nonatomic, retain)MovieDetailView *movieDV;

@property (nonatomic, retain)UIScrollView *myScroll;

@property (nonatomic, retain)NSMutableArray *movieDetailArray;

@property (nonatomic, retain)MovieDetail *movieDetail;

@end

@implementation MovieDetailViewController

- (void)dealloc
{
    [_movieDV release];
    [_myScroll release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.myScroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    self.myScroll.contentSize = CGSizeMake(375, 1000);
    [self.view addSubview:self.myScroll];
    [self.myScroll release];
    
    self.movieDV = [[MovieDetailView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.myScroll addSubview:self.movieDV];
    [self.movieDV release];



//    NSLog(@"%@",self.movieId);
    
    //渲染
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *rightImage = [UIImage imageNamed:@"btn_nav_share.png"];
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction:)];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@", self.movieId];
    
    [AppTools getDataWithURLStr:urlStr Block:^(id consequnce) {
        
        NSMutableDictionary *bigDic = consequnce;
        self.movieDetailArray = [NSMutableArray array];
        NSMutableDictionary *dic = [bigDic objectForKey:@"result"];
        self.movieDetail = [[MovieDetail alloc] init];
        [self.movieDetail setValuesForKeysWithDictionary:dic];
        self.navigationItem.title = self.movieDetail.title;

        self.movieDV.movieD = self.movieDetail;
        [self.movieDetailArray addObject:self.movieDetail];
        [self.movieDetail release];
        
#pragma mark -- 计算内容高度第二步:计算label高度
        CGFloat height = [MovieDetailView heightForContent:self.movieDV.plot_simple.text];
        NSLog(@"%@", self.movieDV.plot_simple.text);
        CGRect tempFrame = self.movieDV.plot_simple.frame;
        tempFrame.size.height = height;
        self.movieDV.plot_simple.frame = tempFrame;
        
        CGFloat height1 = [MovieDetailView heightForContent:self.movieDetail.plot_simple] + 440;
        self.myScroll.contentSize = CGSizeMake(375, height1);

        
    }];
    
    ;
    
    
}

- (void)leftButtonAction:(UIBarButtonItem *)btn{

    [self.navigationController popViewControllerAnimated:1];
}

- (void)rightButtonAction:(UIBarButtonItem *)btn{
    
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
    if ([isLogin isEqualToString:@"1"]) {
        NSMutableArray *array = [[DataBase shareDataBase] selectMovieDetail:0];

    BOOL result = [array containsObject:self.movieDV.movieD.title];
//    NSLog(@"-----%@",result);
    if (result == 0) {
        //    插入数据库
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO movieDetail(title, movieId, userName) VALUES('%@', '%@', '%@')", self.movieDV.movieD.title, self.movieId, [[NSUserDefaults standardUserDefaults] objectForKey:@"name"]];
        [[DataBase shareDataBase] insertSql:insertSql];
       
        //提示框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:1];
        [self presentViewController:alert animated:1 completion:^{
            
            
        }];
        UIAlertAction *sureAct = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        [alert addAction:sureAct];
        
    }
        
        //提示框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏过" preferredStyle:1];
        [self presentViewController:alert animated:1 completion:^{
            
            
        }];
        UIAlertAction *sureAct = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        [alert addAction:sureAct];

    }
        
    UserLoginViewController *loginVC = [[UserLoginViewController alloc] init];
    UINavigationController *loginNavi = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [self presentViewController:loginNavi animated:1 completion:^{
        
        
    }];
    [loginNavi release];
    [loginVC release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
